//
//  ApplicationViewController.m
//  changeView
//
//  Created by Marcelo Perretta on 20/06/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ApplicationViewController.h"
#import "MuchoViewController.h"
#import "CheckConnections.h"

#import "UserData.h"

@implementation ApplicationViewController

@synthesize muchoApp = _muchoApp;

@synthesize username = _username;
@synthesize phonenumber = _phonenumber;

@synthesize user = _user;

@synthesize activityIndicatorView = _activityIndicatorView;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _user = [[UserData alloc] init];
    
    // Get the stored data before the view loads
    NSString *userName = [[_user getUser] objectForKey:@"username"];
    NSString *phoneNumber = [[_user getUser] objectForKey:@"phonenumber"];
    
    if(userName != nil && phoneNumber != nil){
        NSLog(@"Mostrar la aplicacion");
        NSLog(@"Username: %@ Phonenumber: %@", userName, phoneNumber);
        [self changeToMuchoView];
    } else {
        NSLog(@"Username: %@ Phonenumber: %@", userName, phoneNumber);
    }
    
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [super dealloc];
}


#pragma mark - Save User Data
-(IBAction)registerUser:(id)sender{
    
    [self showPreload];
    // Hide the keyboard
    [_username resignFirstResponder];
    [_phonenumber resignFirstResponder];
    
    
    // Create strings and integer to store the text info
    NSString *userName = [_username text];
    NSString *phoneNumber  = [_phonenumber text];
    
    
    if([_user saveDataUser:userName andPass:phoneNumber]){
        NSLog(@"Data Guardada");
        [self showPreload];
        [self changeToMuchoView];

    } else {
        NSLog(@"Error");

    }
}

-(void) changeToMuchoView{
    MuchoViewController *muchoappView = [[MuchoViewController alloc] initWithNibName:@"MuchoViewController" bundle:nil];
    self.muchoApp = muchoappView;
    
    [self.view insertSubview:muchoappView.view atIndex:3];
    [muchoappView release];
    [self hidePreload];
}

-(void) showPreload{
    // Setting Up Activity Indicator View
    self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.activityIndicatorView.hidesWhenStopped = YES;
    self.activityIndicatorView.center = self.view.center;
    [self.view addSubview:self.activityIndicatorView];
    [self.activityIndicatorView startAnimating];
}

-(void) hidePreload{
    [self.activityIndicatorView stopAnimating];
}

@end
